Public Class UserList
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents dgUserList As System.Web.UI.WebControls.DataGrid
    Protected WithEvents PageSelect1 As SR.WebControls.PageSelect
    Protected WithEvents PageSelect2 As SR.WebControls.PageSelect
    Protected WithEvents lblTotalNumber As System.Web.UI.WebControls.Label
    Protected WithEvents lblTotalLabel As System.Web.UI.WebControls.Label
    Protected WithEvents removeUserID As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Header1 As Header
    Protected USER_TYPE As Byte = 1
    Private PAGE_SIZE As Integer = 20


#Region " Properties"
    Private Property Url() As String
        Get
            Return CStr(ViewState("Url"))
        End Get
        Set(ByVal Value As String)
            ViewState("Url") = Value
        End Set
    End Property

    Private Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property

    Private Property SortExpression() As String
        Get
            If (ViewState("SortExpression") Is Nothing) Then
                Return "NAME"
            Else
                Return CStr(ViewState("SortExpression"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortExpression") = Value
        End Set
    End Property

    Private Property SortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                Return " ASC"
            Else
                Return CStr(ViewState("SortDirection"))
            End If
        End Get
        Set(ByVal Value As String)
            ViewState("SortDirection") = Value
        End Set
    End Property
#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initilize the page here
        If Not Me.IsPostBack Then
            Security.InternalAndAdminOnly()
            Me.Url = Request.Url.PathAndQuery
            Me.UrlReferrer = Request.UrlReferrer.PathAndQuery

            'Assign header
            If MIMS_WRS.Web.Security.IsAdmin Then
                Header1.NavigationBar.Add("List of Users", Me.Url)
                Header1.NavigationBar.Add("Create new", "../Admin/User.aspx?usertype=1")
                Header1.NavigationBar.Add("List of Customers", "../Customer/CustomerList.aspx")
                Header1.NavigationBar.Add("Create new", "../Admin/User.aspx?usertype=0")
            Else                
                Header1.NavigationBar.Add("List of Customers", "../Customer/CustomerList.aspx")                
                Header1.NavigationBar.Add("Create new", "../Admin/User.aspx?usertype=0")
            End If

            Header1.MenuIndex = EMenuIndex.InternalUserList

            Me.PageSelect1.PageSize = Me.PAGE_SIZE
            Me.PageSelect2.PageSize = Me.PageSelect1.PageSize

            Me.BindGrid()
        End If
    End Sub

    Private Sub BindGrid()

        'Build datagrid
        Dim objUser As New CUser
        Dim ds As DataSet = objUser.GetUserListByUsertype(Me.USER_TYPE, Me.PageSelect1.CurrentPageIndex * Me.PageSelect1.PageSize, Me.PageSelect1.PageSize)

        'Bind Grid
        Dim dt As DataTable = ds.Tables(0)
        Session("UserList") = dt
        Me.dgUserList.DataSource = dt
        Me.dgUserList.DataBind()

        'Count record
        Dim count As Integer = CInt(ds.Tables(1).Rows(0)(0))
        Me.lblTotalNumber.Text = count.ToString()
        'If count > 1 Then
        '    Me.lblTotalLabel.Text = "USERS"
        'Else
        Me.lblTotalLabel.Text = "USER(S)"
        'End If

        'create top page select
        Me.PageSelect1.ItemCount = count
        Me.PageSelect1.Create()

        'create bottom page select and sync with top page select
        Me.PageSelect2.ItemCount = Me.PageSelect1.ItemCount
        Me.PageSelect2.Create()

    End Sub

    Private Sub dgUserList_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgUserList.SortCommand
        ' Retrieve the data source from session state.
        Dim dt As DataTable = CType(Session("UserList"), DataTable)

        ' Create a DataView from the DataTable.
        Dim dv As DataView = New DataView(dt)

        ' The DataView provides an easy way to sort. Simply set the
        ' Sort property with the name of the field to sort by.
        If e.SortExpression <> Me.SortExpression Then
            dv.Sort = e.SortExpression & " ASC"
            Me.SortExpression = e.SortExpression
            Me.SortDirection = " DESC"
        Else
            dv.Sort = e.SortExpression & Me.SortDirection
            If (Me.SortDirection = " DESC") Then
                Me.SortDirection = " ASC"
            Else
                Me.SortDirection = " DESC"
            End If
        End If
        ' Rebind the data source and specify that it should be sorted
        ' by the field specified in the SortExpression property.
        dgUserList.DataSource = dv
        dgUserList.DataBind()
    End Sub

    Private Sub PageSelect1_PageIndexChanged(ByVal newPageIndex As Integer) Handles PageSelect1.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.PageSelect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Private Sub PageSelect2_PageIndexChanged(ByVal newPageIndex As Integer) Handles PageSelect2.PageIndexChanged
        Me.PageSelect1.CurrentPageIndex = newPageIndex
        Me.PageSelect2.CurrentPageIndex = newPageIndex
        Me.BindGrid()
    End Sub

    Protected Sub RemoveUser_Command(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim userID As System.Int32
        userID = CInt(removeUserID.Value)
        Dim obj As New CUser
        obj.Load(userID)
        obj.Remove()
        Response.Redirect(Me.Url)
    End Sub
End Class
